package fr.afcepf.atod19.voyage.dao.implementation;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Component;

import fr.afcepf.atod19.voyage.dao.interfaces.IDataReservation;
import fr.afcepf.atod19.voyage.entities.Administrateur;
import fr.afcepf.atod19.voyage.entities.Client;
import fr.afcepf.atod19.voyage.entities.Reservation;
import fr.afcepf.atod19.voyage.entities.Voyage;

@Component
public class DaoReservation implements IDataReservation {

	@PersistenceContext
	private EntityManager entityManager;

	@Override
	public Reservation ajouterReservation(Reservation r) {
		
		
			Reservation r2 = new Reservation();
			
			if(null != r && null != r.getClient() ){
				Client c = r.getClient();
				System.out.println("Dans Ajouter Reservation" + c.getIdClient());
				if(c.getIdClient() != 0)
					entityManager.merge(c);
			}
			
			if(null != r && null != r.getVoyage()){
				Voyage v = r.getVoyage();
				if(v.getIdVoyage() != 0)
					entityManager.merge(v);
			}
			
			entityManager.persist(r2);
			
			r2.setDateAnnulation(r.getDateAnnulation());
			r2.setDatePaiement(r.getDatePaiement());
			r2.setEtat(r.getEtat());
			r2.setAdministrateur(r.getAdministrateur());
			r2.setDateReservation(r.getDateReservation());
			r2.setNumeroReservation(r.getNumeroReservation());
			r2.setClient(r.getClient());
			r2.setVoyage(r.getVoyage());
			entityManager.merge(r2);
		
		
		return r;
	}

	@Override
	public Reservation consulterReservation(int idReservation) {
		Reservation r = null;
		r = entityManager.find(Reservation.class, idReservation);
		return r;
	}

	@Override
	public Reservation modifierReservation(Reservation r) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean supprimerReservation(Integer numReservation) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public List<Reservation> rechercherReservation(Reservation r) {
		// TODO Auto-generated method stub
		return null;
	}
	// TEST
}
